package com.genntii.examManager.controller;

import com.genntii.examManager.common.constant.CommonConstant;
import com.genntii.examManager.common.result.PageResult;
import com.genntii.examManager.common.result.Result;
import com.genntii.examManager.domain.vo.CourseDetailVO;
import com.genntii.examManager.domain.vo.CourseVO;
import com.genntii.examManager.service.CourseRecordService;
import com.genntii.examManager.service.CourseService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.apache.ibatis.annotations.Delete;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;

@SuppressWarnings("rawtypes")
@RequestMapping("/course")
@RestController
public class CourseController {

    @Resource
    private CourseService courseService;

    @Resource
    private CourseRecordService courseRecordService;

    @GetMapping("/detail/{courseId}")
    public Result<CourseDetailVO> selectCourseDetail(@PathVariable Long courseId,
                                                     HttpServletRequest request) {
        Long userId = request.getAttribute(CommonConstant.USER_ID) != null ? Long.valueOf(String.valueOf(request.getAttribute(CommonConstant.USER_ID))) : Long.valueOf(String.valueOf(request.getAttribute(CommonConstant.TEACHER_ID)));
        return Result.success(courseService.getCourseDetailById(courseId, userId));
    }

    @GetMapping
    public Result<PageResult<CourseVO>> getCourseVO(@RequestParam("pageNum") Integer pageNum,
                                                    @RequestParam("pageSize") Integer pageSize,
                                                    HttpServletRequest request) {
        Long userId = Long.valueOf(String.valueOf(request.getAttribute(CommonConstant.USER_ID)));
        return Result.success(courseService.getCourseVOList(pageNum, pageSize, userId));
    }

    @GetMapping("/search")
    public Result<PageResult<CourseVO>> searchCourseVO(@RequestParam("pageNum") Integer pageNum,
                                                       @RequestParam("pageSize") Integer pageSize,
                                                       @RequestParam("keyword") String keyword,
                                                       HttpServletRequest request) {
        Long userId = Long.valueOf(String.valueOf(request.getAttribute(CommonConstant.USER_ID)));
        return Result.success(courseService.selectCourseVOList(keyword, pageNum, pageSize, userId));
    }

    @GetMapping("/latest")
    public Result<PageResult<CourseVO>> getCourseLatest(@RequestParam("pageNum") Integer pageNum,
                                                        @RequestParam("pageSize") Integer pageSize,
                                                        HttpServletRequest request) {
        Long userId = Long.valueOf(String.valueOf(request.getAttribute(CommonConstant.USER_ID)));
        return Result.success(courseService.getLatestCourseList(pageNum, pageSize, userId));
    }

    @PostMapping("/partIn/{courseId}")
    public Result takePartIn(@PathVariable Long courseId,
                             HttpServletRequest request) {
        Long userId = Long.valueOf(String.valueOf(request.getAttribute(CommonConstant.USER_ID)));
        courseRecordService.takePartInCourse(courseId, userId);
        return Result.success();
    }

    @GetMapping("/partIn/list")
    public Result<PageResult<CourseVO>> getCourseListUserPartIn(@RequestParam("pageNum") Integer pageNum,
                                                                @RequestParam("pageSize") Integer pageSize,
                                                                HttpServletRequest request) {
        Long userId = request.getAttribute(CommonConstant.USER_ID) != null ? Long.valueOf(String.valueOf(request.getAttribute(CommonConstant.USER_ID))) : Long.valueOf(String.valueOf(request.getAttribute(CommonConstant.TEACHER_ID)));
        return Result.success(courseService.getCourseListUserPartIn(pageNum, pageSize, userId));
    }

    @DeleteMapping("/leave/{courseId}")
    public Result leaveCourse(@PathVariable Long courseId,
                              HttpServletRequest request){
        Long userId = Long.valueOf(String.valueOf(request.getAttribute(CommonConstant.USER_ID)));
        courseRecordService.expel(courseId,userId);
        return Result.success();
    }

}
